<?php


namespace App\Http\Controllers\StoreCenter;


use App\Exceptions\ValidatorException;
use App\Http\Controllers\Controller;
use App\Http\Repositories\Repository;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

class OrderController extends Controller
{
    private $repository;
    public function __construct(Repository $repository)
    {
        $this->repository = $repository;
    }

    public function getOrderInfo(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required_without:order_code',
            'order_code' => 'required_without:id|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getOrderInfo($validator->getData());
    }

    public function getOrderList(Request $request)
    {
        //验证规则
        $rules = [
            'order_code' => 'nullable|string',
            'spu' => 'nullable|string',
            'sku' => 'nullable|string',
            'create_time' => 'nullable|array',
            'status' => 'nullable|int',
            'receiver' => 'nullable|string',
            'country' => 'nullable|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->getOrderList($validator->getData());
    }

    public function addOrder(Request $request)
    {
        //验证规则
        $rules = [
            'total_fee' => 'required|numeric',
            'actual_fee' => 'required|numeric',
            'reduce_fee' => 'required|numeric',
            'shipping_fee' => 'required|numeric',
            'coupon' => 'nullable|string',
            'receiver' => 'required|string',
            'email' => 'required|email',
            'mobile' => 'required|string',
            'country' => 'required|string',
            'province' => 'required|string',
            'city' => 'required|string',
            'county' => 'required|string',
            'address' => 'required|string',

            'sku_info' => 'required|array',
            'sku.*.spu' => 'required|string',
            'sku.*.sku' => 'required|string',
            'sku.*.attribute' => 'required|string',
            'sku.*.sku_info' => 'required|array',
            'sku.*.amount' => 'required|int',
            'sku.*.price' => 'required|numeric',
            'sku.*.actual_price' => 'required|numeric',
            'sku.*.discount' => 'required|numeric',
            'sku.*.shipping_fee' => 'required|numeric',
            'sku.*.summary' => 'required|numeric',
            'sku.*.payment' => 'required|string',
            'sku.*.shipping_method' => 'required|string',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->addOrder($validator->getData());
    }

    public function editOrder(Request $request)
    {
        //验证规则
        $rules = [
            'id' => 'required|int',
            'status' => 'required|int'
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->editOrder($validator->getData());
    }

    public function delOrder(Request $request)
    {
        //验证规则
        $rules = [
            'ids' => 'required',
        ];
        //验证错误信息自定义
        $messages = [

        ];
        //验证结果 错误就抛出异常
        $validator = Validator::make($request->all(), $rules, $messages);
        if ($validator->fails()) {
            throw new ValidatorException($validator->errors()->all());
        }

        return $this->repository->delOrder($validator->getData());
    }
}
